<template>
  <div class="content">
    <div style="width:1000px">
      <div class="content_head">
        <el-button
          type="primary"
          size="mini"
          @click="visiable=true,form.name='',form.color='#409EFF'"
        >添加</el-button>
      </div>
      <el-table :data="tableData" border style="margin-top:20px">
        <el-table-column label="id">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="颜色">
          <template slot-scope="scope">
            <span :style="{backgroundColor:scope.row.color}">&nbsp;&nbsp;&nbsp;&nbsp;</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ getDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="编辑标签" center :visible.sync="visiable1" width="500px">
      <el-form :model="form" ref="form1" :rules="rules" label-width="50px" size="small">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" style="width:310px" clearable></el-input>
          <el-button type="primary" @click="submit1('form1')" style="margin-left:20px">确定</el-button>
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.color"></el-color-picker>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="添加标签" center :visible.sync="visiable" width="500px">
      <el-form :model="form" ref="form" :rules="rules" label-width="50px" size="small">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" style="width:310px" clearable></el-input>
          <el-button type="primary" @click="submit('form')" style="margin-left:20px">确定</el-button>
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.color"></el-color-picker>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { update, add, list, del } from "@/api/label.js";
import { getDate } from "@/utils/dateUtil.js";
export default {
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入标签名称", trigger: "blur" }]
      },
      form: {
        name: "",
        color: "#409EFF"
      },
      visiable1: false,
      visiable: false,
      tableData: []
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    submit1(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(this.form.id, this.form.name, this.form.color).then(res => {
            this.$message({
              type: "success",
              message: res.message
            });
            this.visiable1 = false;
            this.getTableData();
            this.$refs[name].resetFields();
          });
        }
      });
    },
    edit(data) {
      this.form.color = data.color;
      this.form.name = data.name;
      this.form.id = data.id;
      this.visiable1 = true;
    },
    del(id) {
      this.$confirm("此操作将删除该标签, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del(id).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getTableData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          add(this.form.name, this.form.color).then(res => {
            this.$message({
              type: "success",
              message: res.message
            });
            this.visiable = false;
            this.getTableData();
            this.$refs[name].resetFields();
          });
        }
      });
    },
    getTableData() {
      list().then(res => {
        this.tableData = res.data;
      });
    },
    getDate(time) {
      return getDate(time);
    }
  }
};
</script>

<style scoped>
.content {
  display: flex;
  justify-content: center;
}
.content_head {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
</style>

